import { observable, action } from 'mobx-miniprogram'
import mall from '@src/store/mall'
import { getStoreMenu, getStoreGoodsList, getDispatchWay, getStoreDetail } from '@src/api/mall/store'
import { addGoodsToCart, getCartGoods, updateCartGoods, batchDeleteItem } from '@src/api/mall/storeCart'
export default observable({
  // 数据字段
    menuData:[],
    twoMenu: [],
    menuSelectedIndex: 0,
    twoMenuSelectedIndex: 0,
    hasDianpujianjie: false,
    lock: false,
    pageNum: 1,
    goodsData: [],
    selectedMenuId: null,
    cartList: [],
    items: [],
    priceCount: 0,
    cartIds: [],
    cartNum: 0,
    storeAboutHeight: 0,
    top: false,
  // actions
  setTop: action(function(top, storeAboutHeight) {
    this.storeAboutHeight = storeAboutHeight
    this.top = top
  }),
  setSelectedMenuId: action(function(selectedMenuId) {
    this.pageNum = 1
    this.goodsData = []
    this.twoMenuSelectedIndex = 0
    this.lock = false
    this.selectedMenuId = selectedMenuId
  }),
  getFetchData: action(function (homePage) {
    console.log(JSON.parse(JSON.stringify(homePage.modules)))
      this.initChange()
      const modules = homePage.modules
      const shangpinfenlei = modules.filter(item => item.type === 'shangpinfenlei')[0]
      const dianpujianjie = modules.filter(item => item.type === 'dianpujianjie').length
      this.hasDianpujianjie = !!dianpujianjie
      if(!shangpinfenlei) return
      const menuIdList = JSON.parse(shangpinfenlei.biz.params)?.menuIdList
      this.getStoreMenuFetchData(menuIdList)
    }),
  //获取分类
  getStoreMenuFetchData: action(function(menuIdList) {
    if (!menuIdList || !menuIdList.length) return
    getStoreMenu(menuIdList)
      .then(res => {
        const { data } = res
        const { type, menuId, goodsMenuForms } = data[this.menuSelectedIndex]
        this.menuData = data
        this.twoMenu = type === 2 ? goodsMenuForms : []
        this.selectedMenuId = type !== 2 ? menuId : goodsMenuForms[0].menuId
        console.log(this.selectedMenuId)
      })
  }),
  //获取商品
  getStoreGoodsList: action(function(selectedMenuId = this.selectedMenuId) {
    if(this.lock || !selectedMenuId) return
    getStoreGoodsList(mall.storeId, { menuId: selectedMenuId, pageNum: this.pageNum, pageSize: 10 })
      .then(res => {
        const { data } = res
        const goodsData = []
        const arr = [...this.goodsData, ...data.list]
        arr.forEach(item => {
          const filter = goodsData.filter(fitem => fitem.goodId === item.goodId).length
          if(!filter) {
            goodsData.push(item)
          }
        })
        data.list.length >= 10 ? (this.pageNum = ++this.pageNum) : (this.lock = true)
        
        this.goodsData = goodsData
      })
  }),
  initChange: action(function() {
    this.menuData = []
    this.twoMenu = []
    this.hasDianpujianjie = false
    this.menuSelectedIndex = 0
    this.twoMenuSelectedIndex = 0
    this.lock = false
    this.pageNum = 1
    this.goodsData = []
    this.selectedMenuId = null
    
  }),
  getCartGoods: action(function() {
    getCartGoods({ cartType: 0, storeId: mall.storeId, type: 'NO_CHOICE' })
      .then(res => {
        this.cartList = res.data
        let cartList = []
        let menuData = JSON.parse(JSON.stringify(this.menuData))
        menuData.forEach(item => { item.count = 0 })
        let map = res.data.map(item => item.items)
        map.forEach(item => { cartList.push(...item) })
        let priceCount = cartList.reduce((prev, cur) => prev + (cur.price * cur.num), 0)
        const cartIds = cartList.map(item => item.goodsId)
        let cartNum = 0;
        cartList.forEach(item => {
          cartNum += item.num
          menuData.forEach(mItem => {
            if (item.menuIdList.includes(mItem.menuId)) {
              mItem.count += item.num
            }

          })
        })
        this.items = cartList
        this.priceCount = priceCount
        this.cartIds = cartIds
        this.menuData = menuData
        this.cartNum = cartNum
      })
  })
})
